| Conditions | 9 |
| Paths | 8 |
| Total Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { List, Map, fromJS } from 'immutable'; |
||
| 3 | export const findTreeNode = ( |
||
| 4 | treeData, |
||
| 5 | path, |
||
| 6 | childIdentifier = 'children', |
||
| 7 | rootIdentifier = 'root' |
||
| 8 | ) => { |
||
| 9 | |||
| 10 | if (!Map.isMap(treeData)) { |
||
| 11 | treeData = fromJS(treeData); |
||
| 12 | } |
||
| 13 | |||
| 14 | if (!List.isList(path)) { |
||
| 15 | path = fromJS(path); |
||
| 16 | } |
||
| 17 | |||
| 18 | const lookingFor = path.last(); |
||
| 19 | |||
| 20 | let node = treeData.get(rootIdentifier); |
||
| 21 | const indexPath = [rootIdentifier]; |
||
| 22 | |||
| 23 | const firstId = path.first(); |
||
| 24 | path = path.shift(); |
||
| 25 | |||
| 26 | if (node.get('id') === firstId) { |
||
| 27 | |||
| 28 | while (path.count() > 0 && node) { |
||
| 29 | |||
| 30 | if (node |
||
| 31 | && node.get('id') !== lookingFor |
||
| 32 | && node.get(childIdentifier)) { |
||
| 33 | |||
| 34 | const nextId = path.first(); |
||
| 35 | path = path.shift(); |
||
| 36 | |||
| 37 | const nodeIndex = node.get(childIdentifier) |
||
| 38 | .findIndex(n => n.get('id') === nextId); |
||
| 39 | node = node.getIn([childIdentifier, nodeIndex]); |
||
| 40 | indexPath.push(childIdentifier); |
||
| 41 | indexPath.push(nodeIndex); |
||
| 42 | } |
||
| 43 | |||
| 44 | else { |
||
| 45 | node = null; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | return { node, indexPath }; |
||
| 51 | }; |
||
| 52 |